home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libd.lha / Lib / Des / CBC_NOOP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-19  |  4.3 KB  |  154 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/cbc_noop.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  *
  11.  * These routines perform encryption and decryption using the DES
  12.  * private key algorithm, or else a subset of it-- fewer inner loops.
  13.  * (AUTH_DES_ITER defaults to 16, may be less.)
  14.  *
  15.  * Under U.S. law, this software may not be exported outside the US
  16.  * without license from the U.S. Commerce department.
  17.  *
  18.  * These routines form the library interface to the des facilities.
  19.  *
  20.  * des_cbc_noop.c    SPECIAL DEBUGGING VERSION
  21.  *
  22.  *    spm    8/85    MIT project athena
  23.  */
  24.  
  25. #ifndef    lint
  26. static char rcsid_cbc_noop_c[] =
  27. "$Header: cbc_noop.c,v 4.7 88/11/15 11:25:09 jtkohl Exp $";
  28. #endif    lint
  29.  
  30. #include <mit_copy.h>
  31. #include <stdio.h>
  32. #include <des.h>
  33.  
  34. extern int des_debug;
  35. extern int des_debug_print();
  36.  
  37. /*
  38.  * Performs DES cipher-block-chaining operation, either encrypting
  39.  * from cleartext to ciphertext, if encrypt != 0 or decrypting from
  40.  * ciphertext to cleartext, if encrypt == 0.
  41.  *
  42.  * The key schedule is passed as an arg, as well as the cleartext or
  43.  * ciphertext.    The cleartext and ciphertext should be in host order.
  44.  *
  45.  * NOTE-- the output is ALWAYS an multiple of 8 bytes long.  If not
  46.  * enough space was provided, your program will get trashed.
  47.  *
  48.  * For encryption, the cleartext string is null padded, at the end, to
  49.  * an integral multiple of eight bytes.
  50.  *
  51.  * For decryption, the ciphertext will be used in integral multiples
  52.  * of 8 bytes, but only the first "length" bytes returned into the
  53.  * cleartext.
  54.  */
  55.  
  56. int
  57. des_cbc_encrypt(in,out,length,key,iv,encrypt)
  58.     des_cblock *in;        /* >= length bytes of inputtext */
  59.     des_cblock *out;        /* >= length bytes of outputtext */
  60.     register long length;    /* in bytes */
  61.     int encrypt;        /* 0 ==> decrypt, else encrypt */
  62.     des_key_schedule key;        /* precomputed key schedule */
  63.     des_cblock *iv;        /* 8 bytes of ivec */
  64. {
  65.     register unsigned long *input = (unsigned long *) in;
  66.     register unsigned long *output = (unsigned long *) out;
  67.     register unsigned long *ivec = (unsigned long *) iv;
  68.  
  69.     unsigned long i,j;
  70.     static unsigned long t_input[2];
  71.     static unsigned long t_output[8];
  72.     static unsigned char *t_in_p = (unsigned char *) t_input;
  73.     static unsigned long xor_0, xor_1;
  74.  
  75.     if (encrypt) {
  76.     t_output[0] = *ivec++;
  77.     t_output[1] = *ivec;
  78.  
  79.     for (i = 0; length > 0; i++, length -= 8) {
  80.         /* get input */
  81.         t_input[0] = *input++;
  82.         t_input[1] = *input++;
  83.         /* zero pad */
  84.         if (length < 8)
  85.         for (j = length; j <= 7; j++)
  86.             *(t_in_p+j)= 0;
  87.  
  88. #ifdef DEBUG
  89.         if (des_debug)
  90.         des_debug_print("clear",length,t_input[0],t_input[1]);
  91. #endif
  92. #ifdef notdef
  93.         /* do the xor for cbc into the temp */
  94.         t_input[0] ^= t_output[0] ;
  95.         t_input[1] ^= t_output[1] ;
  96.         /* encrypt */
  97. #endif
  98.         /* NO xor for debugging */
  99.         /* encrypt */
  100.         des_ecb_encrypt(t_input,t_output,key,encrypt);
  101.         /* copy temp output and save it for cbc */
  102.         *output++ = t_output[0];
  103.         *output++ = t_output[1];
  104. #ifdef DEBUG
  105.         if (des_debug) {
  106.         des_debug_print("xor'ed",i,t_input[0],t_input[1]);
  107.         des_debug_print("cipher",i,t_output[0],t_output[1]);
  108.         }
  109. #endif
  110.     }
  111.     return 0;
  112.     }
  113.  
  114.     else    /* decrypt */
  115.     {
  116.     xor_0 = *ivec++;
  117.     xor_1 = *ivec;
  118.  
  119.     for (i = 0; length > 0; i++, length -= 8) {
  120.         /* get input */
  121.         t_input[0] = *input++;
  122.         t_input[1] = *input++;
  123.         /* no padding for decrypt */
  124. #ifdef DEBUG
  125.         if (des_debug)
  126.         des_debug_print("cipher",i,t_input[0],t_input[1]);
  127. #endif
  128.         /* encrypt */
  129.         des_ecb_encrypt(t_input,t_output,key,encrypt);
  130. #ifdef DEBUG
  131.         if (des_debug)
  132.         des_debug_print("out pre xor",i,t_output[0],t_output[1]);
  133. #endif
  134. #ifdef notdef
  135.         /* do the xor for cbc into the output */
  136.         t_output[0] ^= xor_0 ;
  137.         t_output[1] ^= xor_1 ;
  138. #endif
  139.         /* NO xor -- debugging */
  140.  
  141.         /* copy temp output */
  142.         *output++ = t_output[0];
  143.         *output++ = t_output[1];
  144.         /* save xor value for next round */
  145.         xor_0 = t_input[0];
  146.         xor_1 = t_input[1];
  147. #ifdef DEBUG
  148.         if (des_debug)
  149.         des_debug_print("clear",i,t_output[0],t_output[1]);
  150. #endif
  151.     }
  152.     }
  153. }
  154.